/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package edu.gatech.ealf.ttsplaf;

import edu.gatech.ealf.AudioOutputManager;
import edu.gatech.ealf.ComponentAccessibilityConfiguration;
import edu.gatech.ealf.EasyAccessibilityLookAndFeel;

import edu.gatech.ealf.TextToSpeechManager;
import java.awt.event.ActionEvent;
import java.awt.event.MouseEvent;
import java.util.Map;
import javax.swing.JComponent;
import javax.swing.JSlider;
import javax.swing.event.ChangeEvent;

/**
 *
 * @author Hemanth Pai
 */
public class TTSHelper {
    
    private String descriptionText = null;

    private String actionText = null;

    private String descriptionFilename = null;

    private String actionFilename = null;
    
    private JComponent curComponent = null;
    
    private TextToSpeechManager speaker = null;
    
    private static Thread thread;
    
    private boolean speak = true;
    
    private static TTSHelper instance = null;
    
    public static TTSHelper getInstance(){
        instance = new TTSHelper();
        return instance;
    }
    private TTSHelper(){
        
    }
    
    public void silence(){
            if(speaker != null){
            	speaker.cancel();
            }
    }
    
    public void speakMessage(String Message){
        if(speak){
            if(speaker != null){
            	speaker.cancel();
            }
            
            this.speaker = new TextToSpeechManager(Message);
            thread = new Thread(this.speaker);
            thread.setName("SpeakerThread");
            thread.start();
        }
    }
    
    public void speakMessageFromFile(String path)
    {
        if(speak){
            AudioOutputManager player = new AudioOutputManager(path);
            player.start();
        }
    }
    
    public void readConfig(JComponent c) {
        String compName = c.getName();
        if (compName != null && !compName.equals("")) {
            ComponentAccessibilityConfiguration compConfig = EasyAccessibilityLookAndFeel.componentConfig
                    .get(compName);
            if(compConfig != null){
                if (compConfig.audioOutputFilename != null
                        && !compConfig.audioOutputFilename.equals("")) {
                    descriptionFilename = compConfig.audioOutputFilename;
                }
                if (compConfig.audioOutputText != null
                        && !compConfig.audioOutputText.equals("")) {
                    descriptionText = compConfig.audioOutputText;
                }
                if (compConfig.audioActionFilename != null
                        && !compConfig.audioActionFilename.equals("")) {
                    actionFilename = compConfig.audioActionFilename;
                }
                if (compConfig.audioActionText != null
                        && !compConfig.audioActionText.equals("")) {
                    actionText = compConfig.audioActionText;
                }
            }
        }
    }
    
    public boolean speakMouseEntered() {
            if (descriptionFilename != null && speak) {
                AudioOutputManager player = new AudioOutputManager(descriptionFilename);
                player.start();
            } else if (descriptionText != null && speak) {
            	if(speaker != null){
                	speaker.cancel();
                }  
                this.speaker = new TextToSpeechManager(descriptionText);
                thread = new Thread(this.speaker);
                thread.setName("SpeakerThread");
                thread.start();
            } else {
                return false;
            }

            return true;
    }
    
    public boolean speakActionPerformed(){
            if (actionFilename != null && speak) {
                AudioOutputManager player = new AudioOutputManager(actionFilename);
                player.start();
            } else if (actionText != null && speak) {
            	if(speaker != null){
                	speaker.cancel();
                }
                this.speaker = new TextToSpeechManager(actionText);
                thread = new Thread(this.speaker);
                thread.start();
                
            } else {
                return false;
            }
            return true;
    }
    
    public void toggleSpeak(){
        speak = !speak;
    }
    
    public void actionPerformed(ActionEvent e){
        if(!speakActionPerformed()){
            readConfig((JComponent)e.getSource());
            speakActionPerformed();
        }
    }
    
    public void mouseEntered(MouseEvent e) {
        if(!speakMouseEntered()){
            readConfig((JComponent)e.getComponent());
            speakMouseEntered();
        }
    }
    
    public void stateChanged(ChangeEvent e) {
        if(!speakActionPerformed()){
            readConfig((JComponent)e.getSource());
            if(!((JSlider)e.getSource()).getValueIsAdjusting())
                speakActionPerformed();
        }
    }
    
    @SuppressWarnings("static-access")
    public void mouseExited() throws InterruptedException{
        silence();
    }
}
